package com.irun.cm;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;


/***
 * @author huangsm
 * @date 2012-5-24
 * @email huangsanm@gmail.com
 * @desc 点击联系人头像显示window
 */
public class QuickContactWindow implements View.OnClickListener {
	//com_android_contacts_com_android_contacts_DialtactsActivity
	private final static String CALL = "com.android.contacts";
	private final static String CALL_PACKAGE = "com.android.contacts.DialtactsActivity";
	//com_android_mms_com_android_mms_ui_conversationlist
	private final static String MESSAGE = "com.android.mms";
	private final static String MESSAGE_PACKAGE = "com.android.mms.ui.conversationlist";
	//com_android_contacts_com_android_contacts_DialtactsContactsEntryActivity
	private final static String VIEW = "com.android.contacts";
	private final static String VIEW_PACKAGE = "com.android.contacts.DialtactsContactsEntryActivity";
	
	private Context mContext;
	private LayoutInflater mInflater;
	
	//是否是陌生人
	private boolean mUnknow;
	
	/**
	 * 陌生人 
	 * @param context
	 * @param isUnknow
	 * @param locationX
	 * @param locationY
	 */
	public QuickContactWindow(Context context, boolean isUnknow){
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		
		mUnknow = isUnknow;
	}
	
	public void show(View view, int x, int y){
		PopupWindow popup = new PopupWindow(view, 200, 120);
		//popup.setWindowLayoutMode(LayoutParams.MATCH_PARENT, 120);
		popup.setContentView(getContextView());
		popup.setOutsideTouchable(true);
		popup.showAsDropDown(view, x, y);//, locatx, locaty
		//popup.showAtLocation(view, Gravity.CENTER, 0, 0);
	}
	
	//初始化PopupWindow中的setcontentview
	private LinearLayout getContextView(){
		PackageManager pm = mContext.getPackageManager();
		Resources res = mContext.getResources();
		Drawable call = null;
		Drawable message = null;
		Drawable unknow = null;
		ActivityInfo info = null;
		String text = "";
		
		try {
			Intent call_intent = new Intent();
			call_intent.setClassName(CALL, CALL_PACKAGE);
			call = pm.getActivityIcon(pm.getLaunchIntentForPackage(CALL));//pm.getActivityIcon(call_intent);
			
			Intent msg_intent = new Intent();
			msg_intent.setClassName(MESSAGE, MESSAGE_PACKAGE);
			message = pm.getActivityIcon(msg_intent);
			System.out.println("munKnow:" + mUnknow);
			if(mUnknow){
				unknow = res.getDrawable(R.drawable.contact_save);
				text = res.getString(R.string.contact_item_savecontact);
			}else{
				Intent view_intent = new Intent();
				view_intent.setClassName(VIEW, VIEW_PACKAGE);
				text = res.getString(R.string.contact_item_view);
				unknow = pm.getActivityIcon(view_intent);
			}
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			if(call == null)
				//call = info.loadIcon(pm);
			if(message == null)
				message = res.getDrawable(R.drawable.contact_message);
		}
		LinearLayout layout = (LinearLayout) mInflater.inflate(R.layout.contact_item, null);
		TextView tv_call = (TextView) layout.findViewById(R.id.contact_item_call);
		tv_call.setCompoundDrawablesWithIntrinsicBounds(null, call, null, null);
		tv_call.setOnClickListener(this);
		
		TextView tv_msg = (TextView) layout.findViewById(R.id.contact_item_message);
		tv_msg.setCompoundDrawablesWithIntrinsicBounds(null, message, null, null);
		tv_msg.setOnClickListener(this);
		
		TextView tv_unknow = (TextView) layout.findViewById(R.id.contact_unkonw);
		tv_unknow.setCompoundDrawablesWithIntrinsicBounds(null, unknow, null, null);
		tv_unknow.setText(text);
		tv_unknow.setOnClickListener(this);
		return layout;
	}

	@Override
	public void onClick(View v) {
		
	}
}
